package com.sweet.fourtimeoa.controller;

import com.github.pagehelper.PageInfo;
import com.sweet.fourtimeoa.pojo.dto.DocumentList;
import com.sweet.fourtimeoa.pojo.dto.DocumentQueryDto;
import com.sweet.fourtimeoa.pojo.dto.PageDto;
import com.sweet.fourtimeoa.pojo.entity.Documents;
import com.sweet.fourtimeoa.pojo.vo.PageVo;
import com.sweet.fourtimeoa.pojo.vo.UplodeVo;
import com.sweet.fourtimeoa.service.DocumentService;
import com.sweet.fourtimeoa.util.BaseContext;
import com.sweet.fourtimeoa.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Tag(name = "文档管理")
@RestController
@RequestMapping("/oa/document")
@CrossOrigin(origins = "*")
public class DocumentController {
    @Autowired
    private DocumentService documentService;

    @Operation(summary = "文档列表_分页查询")
    @PostMapping("/pagingSel")
    public Result DocumentList(@RequestBody PageDto<DocumentQueryDto> pageDto) {
        PageVo<DocumentList> documentList = documentService.getDocumentList(pageDto);
        return Result.success(documentList);
    }
    @Operation(summary = "文档回收站_分页查询")
    @GetMapping("/recycleBinSel")
    public Result recycleBinSel(PageDto pageDto) {
        PageVo<DocumentList> documentList = documentService.getDocumentRecycleBin(pageDto.getPageNum(), pageDto.getPageSize());
        return Result.success(documentList);
    }
    @Operation(summary = "文档回收站_还原")
    @PutMapping("/recycleBinRestore/{documentId}")
    public Result recycleBinRestore(@PathVariable Integer documentId) {
        documentService.updateDocumentRecycleBin(documentId);
        return Result.success();
    }
    @Operation(summary = "文档列表_删除")
    @DeleteMapping("/deleteDocument/{documentId}")
    public Result deleteDocument(@PathVariable Integer documentId) {
        documentService.deleteDocument(documentId);
        return Result.success();
    }
    @Operation(summary = "文档回收站_删除")
    @DeleteMapping("/deleteDocumentRecycleBin/{documentId}")
    public Result deleteDocumentRecycleBin(@PathVariable Integer documentId) {
        documentService.deleteDocumentRecycleBin(documentId);
        return Result.success();
    }

    //文件上传
    @Operation(summary = "文档列表_上传")
    @PostMapping("/upload")
    public Result upload(@RequestParam MultipartFile file) throws IOException {
        BaseContext.setCurrentId(BaseContext.getCurrentId());
        //判断文件是否为空
        if (file.isEmpty()) {
            return Result.error("文件为空");
        }
        //获取文件名
        String fileName = file.getOriginalFilename();
        log.info("文件名：{}", fileName);
        //获取文件后缀
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //文件上传路径
        String filePath = "D://1//";
        File dest = new File(filePath + fileName);
        //判断文件父目录是否存在
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdir();
        }
        //保存文件
        file.transferTo(dest);


        UplodeVo uplodeVo = new UplodeVo();
        uplodeVo.setFileName(fileName);
        uplodeVo.setFilePath(filePath);
        return Result.success(uplodeVo);
    }

    //保存文件
    @Operation(summary = "文档列表_保存")
    @PostMapping("/saveDocument")
    public Result saveDocument(@RequestBody Documents documents) {
        documents.setCreatedAt(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        documentService.saveDocument(documents);
        return Result.success();
    }


//    //根据路径删除本地文件
//    @Operation(summary = "文档列表_删除本地文件")
//    @DeleteMapping("/deleteDocumentRecycleBin")
//    public  void deleteFile(String filePath, String fileName) {
//        //根据文件路径和文件名删除文件
//        File file = new File(filePath + fileName);
//        log.info("文件路径：{}", filePath);
//        if (file.exists()) {
//            file.delete();
//        }
//    }
}
